import time

from selenium.webdriver.common.by import By

from common import utils


class ProductListingBusiness:
    def administrator_login(self, driver, username, password):
        driver.find_element(by=By.CSS_SELECTOR, value='[name="username"]').clear()
        driver.find_element(by=By.CSS_SELECTOR, value='[name="username"]').send_keys(username)
        driver.find_element(by=By.CSS_SELECTOR, value='[name="password"]').clear()
        driver.find_element(by=By.CSS_SELECTOR, value='[name="password"]').send_keys(password)
        driver.find_element(by=By.CSS_SELECTOR, value=".el-button").click()

    def enter_product_list(self, driver):
        driver.find_element(by=By.XPATH, value="/html/body/div/div/div[1]/div[1]/div/ul/div[4]/li/div").click()
        # time.sleep(2)
        driver.find_element(by=By.XPATH, value="/html/body/div[1]/div/div[1]/div[1]/div/ul/div[4]/li/ul/div[2]/a/li").click()

    def add_product(self,driver, number, name, price, detail):
        driver.find_element(by=By.XPATH, value="/html/body/div[1]/div/div[2]/section/div/div[1]/div/form/div[1]/div/div/input").send_keys(number)
        driver.find_element(by=By.XPATH, value="/html/body/div[1]/div/div[2]/section/div/div[1]/div/form/div[2]/div/div/input").send_keys(name)
        driver.find_element(by=By.XPATH, value="/html/body/div[1]/div/div[2]/section/div/div[1]/div/form/div[3]/div/div/input").send_keys(price)
        driver.find_element(by=By.XPATH, value="/html/body/div[1]/div/div[2]/section/div/div[1]/div/form/div[4]/div/div/label[1]/span[1]/span").click()
        driver.find_element(by=By.XPATH, value="/html/body/div[1]/div/div[2]/section/div/div[1]/div/form/div[5]/div/div/label[2]/span[1]/span").click()
        driver.find_element(by=By.XPATH, value="/html/body/div[1]/div/div[2]/section/div/div[1]/div/form/div[6]/div/div/label[1]/span[1]/span").click()
        # # 使用了frame框架,切换到指定的 frame页面
        frame_element = driver.find_element(by=By.CSS_SELECTOR, value="[title='在编辑区按ALT-F9打开菜单，按ALT-F10打开工具栏，按ALT-0查看帮助']")
        driver.switch_to.frame(frame_element)
        driver.find_element(by=By.XPATH, value="/html/body/p").send_keys(detail)
        # # 退出frame,切换到默认页面
        driver.switch_to.default_content()
        driver.find_element(by=By.XPATH, value="/html/body/div[1]/div/div[2]/section/div/div[5]/button[2]").click()

if __name__ == '__main__':
    plb = ProductListingBusiness()

    # 超级管理员登录
    driver = utils.web_driver()
    plb.administrator_login(driver,"admin123","admin123")
    # time.sleep(3)
    plb.enter_product_list(driver)
    plb.add_product(driver, "1001001", "测试商品001", "1000", "测试商品详情xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
    utils.driver_quit(driver)

    # # 商城管理员登录
    # driver1 = utils.web_driver()
    # plb.administrator_login(driver1, "mall123", "mall123")
    # utils.driver_quit(driver1)
    #
    # # 推广管理员登录
    # driver2 = utils.web_driver()
    # plb.administrator_login(driver2, "promotion123", "promotion123")
    # utils.driver_quit(driver2)